import api from '../../api';
import { message } from 'antd';
import moment from 'moment';
export default {
  namespace: 'OutsourceInvestMsgMaintainM',

  state: {
    assetType: undefined, // 资产类别
    fundActualInvest: undefined, // 资金实际投向
    outsourceInvestProperty: undefined, // 委外投资属性
    fundUseIndustry: undefined, // 资金运用行业
    splitWayList: [], //拆分方式
  },
  effects: {
    // 获取委外投资信息维护列表
    *getTableData({ payload, callback }, { call, put, select }) {
      const res = yield call(api.outsourceInvestMsgMaintain.getTableData, payload);
      let fundUseIndustryData = yield select((state) => state.OutsourceInvestMsgMaintainM.fundUseIndustry);
      let assetTypeData = yield select((state) => state.OutsourceInvestMsgMaintainM.assetType);
      let outsourceInvestPropertyData = yield select((state) => state.OutsourceInvestMsgMaintainM.outsourceInvestProperty);

      if (res && res.code == 0) {
        // 前端翻译
        res.data?.dataSource &&
          res.data?.dataSource.length &&
          res.data?.dataSource.forEach((item) => {
            // 资金运用行业
            fundUseIndustryData?.length &&
              fundUseIndustryData.forEach((fundItem) => {
                if (fundItem.list?.length) {
                  let tempResult = fundItem.list.find((i) => i.value == item.fundUseIndustry);
                  if (tempResult) {
                    item.fundUseIndustry = tempResult.title;
                  }
                }
              });
            // 资产类别
            let assetTemp = assetTypeData.find((i) => i.value == item.assetType);
            item.assetType = assetTemp?.title ? assetTemp?.title : item.assetType;
            // 委外投资属性
            let outsourceTemp = outsourceInvestPropertyData.find((i) => i.value == item.outsourceInvestProperty);
            item.outsourceInvestProperty = outsourceTemp?.title ? outsourceTemp?.title : item.outsourceInvestProperty;

            // 投资计划终止日期和成立日期 日期格式处理
            item.investPlanTerminateDate = item.investPlanTerminateDate ? moment(item.investPlanTerminateDate) : null;
            item.investPlanSetupDate = item.investPlanSetupDate ? moment(item.investPlanSetupDate) : null;
          });
        // 金额展示处理
        res.data?.columns &&
          res.data?.columns.length &&
          res.data?.columns.forEach((item) => {
            if (item.key == 'money') {
              // 金额
              item.type = 'money';
            }
            if (item.key == 'investPlanTerminateDate' || item.key == 'investPlanSetupDate') {
              // 投资计划成立和终止日期
              item.type = 'date';
            }
            if (item.key == 'shareFaceValue') {
              // 份额面额
              item.toFixed = 4;
              item.type = 'money';
            }
          });
        callback('success', res.data);
      } else {
        callback('fail');
        message.error(`获取非标估值列表失败，失败原因：${res?.msg}`);
        console.error(`获取非标估值列表失败，失败原因：${res?.msg}`, '接口名称： findAll 接口：portfolio-server/entrustedinvestment/findall');
      }
    },
    // 获取行业的枚举接口
    *getFundDictionList({ payload, callback }, { call, put, select }) {
      let response = yield select((state) => state.globalM.dataDictionary['GJTJJHYFLI']);
      if (response && response.length > 0) {
        // 资金运用行业
        let tempFundUseIndustry = [];
        tempFundUseIndustry = response.map((item) => {
          return {
            label: item.value,
            list: item?.children.map((x) => {
              return {
                value: x.code,
                title: `${x.code}-${x.value}`,
              };
            }),
          };
        });
        yield put({
          type: 'save',
          payload: {
            fundUseIndustry: tempFundUseIndustry, //资金运用行业
          },
        });
      } else {
        message.error(`获取字典失败，失败原因：`);
      }
    },
    // 获取枚举接口
    *getDictionList({ payload, callback }, { call, put }) {
      const response = payload['dictData'];
      if (response) {
        // 资产类别
        let temAssetType = [];
        response.BAXX_ZCLB &&
          response.BAXX_ZCLB.length > 0 &&
          response.BAXX_ZCLB.forEach((item) => {
            temAssetType.push({ title: item.value, value: item.code });
          });
        // 资金实际投向
        let tempFundActualInvest = [];
        response.fundActualInvest &&
          response.fundActualInvest.length > 0 &&
          response.fundActualInvest.forEach((item) => {
            tempFundActualInvest.push({ title: item.value, value: item.code });
          });
        // 委外投资属性
        let tempOutsourceInvestProperty = [];
        response.outsourceInvestProperty &&
          response.outsourceInvestProperty.length > 0 &&
          response.outsourceInvestProperty.forEach((item) => {
            tempOutsourceInvestProperty.push({ title: item.value, value: item.code });
          });
        // 拆分方式
        let splitWayList = [];
        response.SPLIT_WAY_DICTIONARY &&
          response.SPLIT_WAY_DICTIONARY.length > 0 &&
          response.SPLIT_WAY_DICTIONARY.forEach((item) => {
            splitWayList.push({ title: item.value, value: item.code });
          });
        yield put({
          type: 'save',
          payload: {
            assetType: temAssetType, // 资产类别
            fundActualInvest: tempFundActualInvest, // 资金实际投向
            outsourceInvestProperty: tempOutsourceInvestProperty, // 委外投资属性
            splitWayList: splitWayList,
          },
        });
      } else {
        message.error(`获取字典失败，失败原因：`);
      }
    },
    // 根据id获取委外投资信息详情
    *getDataById({ payload, callback }, { call, put }) {
      const res = yield call(api.outsourceInvestMsgMaintain.getDataById, payload);
      if (res && res.code == 0) {
        callback('success', res.data.dataSource);
      } else {
        callback('fail');
        message.error(`根据id获取委外投资信息详情失败，失败原因：${res?.msg}`);
        console.error(`根据id获取委外投资信息详情失败，失败原因：${res?.msg}`, '接口名称： getinfoById 接口：portfolio-server/entrustedinvestment/getinfoById');
      }
    },
    // 删除数据
    *deleteById({ payload, callback }, { call, put }) {
      const res = yield call(api.outsourceInvestMsgMaintain.deleteById, payload);
      if (res && res.code == 0) {
        callback('success');
        message.success('删除成功');
      } else {
        callback('fail');
        message.error(`删除失败，失败原因：${res?.msg}`);
        console.error(`删除失败，失败原因：${res?.msg}`, '接口名称： delete 接口：portfolio-server/entrustedinvestment/delete');
      }
    },
    // 新增委外投资
    *saveData({ payload, callback }, { call, put }) {
      const res = yield call(api.outsourceInvestMsgMaintain.saveData, payload);
      if (res && res.code == 0) {
        callback('success');
        message.success('保存委外投资成功');
      } else {
        callback('fail');
        message.error(`保存委外投资失败，失败原因：${res?.msg}`);
        console.error(`保存委外投资失败，失败原因：${res?.msg}`, '接口名称： save 接口：portfolio-server/entrustedinvestment/save');
      }
    },
    // 编辑委外投资
    *editData({ payload, callback }, { call, put }) {
      const res = yield call(api.outsourceInvestMsgMaintain.editData, payload);
      if (res && res.code == 0) {
        callback('success');
        message.success('编辑委外投资成功');
      } else {
        callback('fail');
        message.error(`保存或编辑委外投资失败，失败原因：${res?.msg}`);
        console.error(`保存或编辑委外投资失败，失败原因：${res?.msg}`, '接口名称： edit 接口：portfolio-server/entrustedinvestment/edit');
      }
    },
    // 展期
    *deferDate({ payload, callback }, { call, put }) {
      const res = yield call(api.outsourceInvestMsgMaintain.deferDate, payload);
      if (res && res.code == 0) {
        message.success('展期成功!');
        callback('success');
      } else {
        callback('fail');
        message.error(`展期失败，失败原因：${res?.message}`);
        console.error(`展期失败，失败原因：${res?.message}`, '接口名称： saveById 接口：portfolio-server/entrustedinvestment/saveById');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
